package com.cicadamob.contact.api.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import com.cicadamob.contact.api.Utils.StringUtil;

import android.os.Environment;


/**
 * 文件处理相关工具类
 * 
 * @author xiaoxh
 * @link 
 * 
 * @version $Revision: 1.00 $ $Date: 2010-04-12
 */

public class DownloadUtils {
	
	public static final String DOWNLOAD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/ManagerAssistant/download/" ;
	public static final int BUFFER_SIZE = 32 * 1024;
	public static final int DOWN_ARRAY_SIZE = 1024 * 32; // 游戏下载缓冲大小
	
	public static boolean isSdcardEnable()
	{
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && 
				createIfNotExist("runlog.txt"))
		{
			return true;
		}
		return false;
	}
	
	/* 自定义删除文件方法 */
	public static void delFile(String strFileName) {
		File myFile = new File(strFileName);
		if (myFile.exists()) {
			myFile.delete();
		}
		myFile = null;
	}
	
	public static void delFile(File file) {
		if(file == null)
			return ;
		if (file.exists()) {
			file.delete();
		}
	}
	
	public static boolean exist(String file)
	{
		boolean exist = false;
		try {
			File tempFile = new File(DOWNLOAD_PATH);
			if(!tempFile.exists())
				tempFile.mkdirs();
			File f = new File(DOWNLOAD_PATH + "/" + file);
			if(f.exists())
			{
				exist = true;
			}
			tempFile = null; f = null;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return exist;
	}
	
	public static boolean existFile(String path, String file)
	{
		boolean exist = false;
		try {
			File tempFile = new File(path);
			if(!tempFile.exists())
				return false;
			File f = new File(path + "/" + file);
			if(f.exists())
			{
				exist = true;
			}
			tempFile = null; f = null;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return exist;
	}
	
	public static boolean isFileCreatedInToday(String path, String file)
	{
		try {
			File tempFile = new File(path);
			if(!tempFile.exists())
				return false;
			File f = new File(path + "/" + file);
			if(f.exists())
			{
				Date date = new Date(f.lastModified());
				if(StringUtil.getStrDate().equals(StringUtil.getStrDate(date)))
				{
					return true;
				}
				date = null;
			}
			tempFile = null; f = null;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return false;
	}
	
	public static boolean createIfNotExist(String file)
	{
		boolean exist = false;
		try {
			File tempFile = new File(DOWNLOAD_PATH);
			if(!tempFile.exists())
				tempFile.mkdirs();
			File f = new File(DOWNLOAD_PATH + "/" + file);
			if(f.exists())
			{
				exist = true;
			}
			else
			{
				FileWriter fw = new FileWriter(DOWNLOAD_PATH + "/" + file);
				fw.write("");
				fw.flush();
				fw.close();
				exist = true;
				fw = null;
			}
			f = null; tempFile = null;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return exist;
	}
	
	public static File getFile(String file)
	{
		File f = null;
		try {
			File tempFile = new File(DOWNLOAD_PATH);
			if(!tempFile.exists())
				tempFile.mkdirs();
			f = new File(DOWNLOAD_PATH + "/" + file);
			tempFile = null;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return f;
	}
	
	public static void renameFile(String source, String dest)
	{
		File tempFile = new File(DOWNLOAD_PATH);
		if(!tempFile.exists())
			tempFile.mkdirs();
		
		FileOutputStream fos = null;
		InputStream is = null;
		BufferedInputStream bis = null;
		try {
			fos = new FileOutputStream(DOWNLOAD_PATH + "/" + dest);
			
			is = new FileInputStream(DOWNLOAD_PATH + "/" + source);
			bis = new BufferedInputStream(is, BUFFER_SIZE);
			
			byte buf[] = new byte[BUFFER_SIZE];
			
			do {
				int numread = bis.read(buf);
				if (numread <= 0) {
					break;
				}
				fos.write(buf, 0, numread);
			}while(true);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(is != null) is.close();
				if(fos != null) fos.close();
				if(bis != null) bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		delFile(DOWNLOAD_PATH + "/" + source);
		tempFile = null; fos = null; is = null; bis = null;
	}
}
